package com.yonyou.pmclouds.projectpart.upgrade.rmiimpl;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogVO;
import com.yonyou.pmclouds.projectpart.entity.ProjectPartVO;
import com.yonyou.pmclouds.projectpart.upgrade.service.IProjectPartBusinessOperator;
import com.yonyou.pmclouds.safetyrectification.entity.SafetyRectificationVO;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author th
 * @date 2020/4/21 11:29
 */
public abstract class AbstractProjectPartBusinessOperator implements IProjectPartBusinessOperator {

    /**
     * 升级前业务单据类型和工程部位名称属性的映射
     */
    private static Map<String, String> billTypeToPartNameMap;

    static {
        billTypeToPartNameMap = new HashMap<>();
        billTypeToPartNameMap.put(BillTypeConst.SAFETY_RECTIFICATION_BILLTYPE, SafetyRectificationVO.RECTIFICATIONAREA);
        billTypeToPartNameMap.put(BillTypeConst.PROBLEMLOG_BILLTYPE, ProblemLogVO.IMPROVEMENT_AREA);
        billTypeToPartNameMap.put(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE, ConstructionLogVO.INSPECTAREA);
        billTypeToPartNameMap.put(BillTypeConst.SITEINSPECTION_BILLTYPE, SiteInspectionVO.INSPECT_AREA);
    }

    @Override
    public List<ProjectPartVO> constructProjectPartVOsOfBill() {
        SuperVO[] billVOs = queryBusinessVOs();
        if (ArrayUtils.isEmpty(billVOs))
            return new ArrayList<>();

        List<ProjectPartVO> insertVOs = new ArrayList<>();
        // 组装工程部位档案
        String pkProject, partName, pkTenant;
        for (SuperVO billVO : billVOs) {
            pkProject = BeanHelper.getProperty(billVO, CommonFieldConst.PKPROJECT) == null ? (String) BeanHelper.getProperty(billVO, CommonFieldConst.PK_PROJECT) :
                    (String) BeanHelper.getProperty(billVO, CommonFieldConst.PKPROJECT);
            partName = (String) BeanHelper.getProperty(billVO, billTypeToPartNameMap.get(getOperateBillType()));
            pkTenant = BeanHelper.getProperty(billVO, CommonFieldConst.PK_TENANT) == null ? (String) BeanHelper.getProperty(billVO, CommonFieldConst.PKTENANT) :
                    (String) BeanHelper.getProperty(billVO, CommonFieldConst.PK_TENANT);

            ProjectPartVO partVO = new ProjectPartVO();
            partVO.setPkTenant(pkTenant);
            partVO.setPkProject(pkProject);
            partVO.setSname(partName);
            insertVOs.add(partVO);
        }

        return insertVOs;
    }

    /**
     * 业务单据实现，查询升级前的工程部位
     *
     * @return
     */
    abstract protected SuperVO[] queryBusinessVOs();

    abstract public String getOperateBillType();
}
